package cn.loioi.test;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.annotation.Px;
import android.support.v7.widget.AppCompatTextView;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.Log;
import android.view.GestureDetector;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.widget.LinearLayout;

import org.xutils.common.util.DensityUtil;

import java.util.ArrayList;
import java.util.List;

import cn.virens.library.uitls.LogUtil;


public class WheelView extends LinearLayout {
    private int wheelShow;
    private int wheelCache;
    private int wheelItemHeight;

    private int wheelTextSize;
    private int wheelTextColor;
    private int wheelTextActiveColor;
    private int wheelLineSize;
    private int wheelLineColor;

    private int selected = 0;
    private List<WheelDisplay> list = new ArrayList<>();
    private GestureDetector mGestureDetector = null;

    public WheelView(Context context) {
        this(context, null);
    }

    public WheelView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public WheelView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        this.init(attrs, defStyle);
        this.setScrollContainer(true);
        this.setFocusableInTouchMode(true);

        for (int i = 0; i < wheelCache; i++) {
            WheelTextView t = new WheelTextView(getContext());
            t.setText("选项-》" + i);

            this.addView(t);
        }
        this.mGestureDetector = new GestureDetector(getContext(), onGestureListener);
        this.scrollTo(0, wheelItemHeight);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return mGestureDetector.onTouchEvent(event) || true;
    }

    public void scrollTo(@Px int y) {
        super.scrollTo(0, y);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(wheelShow * wheelItemHeight, MeasureSpec.EXACTLY));
    }

    private void init(AttributeSet attrs, int defStyle) {
        final TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.WheelView, defStyle, 0);

        this.wheelShow = a.getInt(R.styleable.WheelView_wheelItemShow, 3);
        this.wheelCache = a.getInt(R.styleable.WheelView_wheelItemCache, 5);
        this.wheelItemHeight = a.getDimensionPixelSize(R.styleable.WheelView_wheelItemHeight, DensityUtil.dip2px(20F));
        this.wheelTextSize = a.getDimensionPixelSize(R.styleable.WheelView_wheelTextSize, DensityUtil.dip2px(14F));
        this.wheelTextColor = a.getColor(R.styleable.WheelView_wheelTextColor, Color.parseColor("#eeeeee"));
        this.wheelTextActiveColor = a.getColor(R.styleable.WheelView_wheelTextActiveColor, Color.parseColor("#333333"));
        this.wheelLineSize = a.getDimensionPixelSize(R.styleable.WheelView_wheelLineSize, DensityUtil.dip2px(2F));
        this.wheelLineColor = a.getColor(R.styleable.WheelView_wheelLineColor, Color.parseColor("#83cde6"));

        a.recycle();
    }

    private boolean _isSelected(int index) {
        return index == selected;
    }


    @Override
    protected LinearLayout.LayoutParams generateDefaultLayoutParams() {
        return new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, wheelItemHeight);
    }

    private final GestureDetector.OnGestureListener onGestureListener = new GestureDetector.SimpleOnGestureListener() {
        private float lastY = 0;
        private int lastScroll = wheelItemHeight;
        private int start = 0;

        @Override
        public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
            int scroll = (int) ((e1.getY() - e2.getY()) % wheelItemHeight) + wheelItemHeight;
            float y1 = e1.getY();
            float y2 = e2.getY();
            if (lastY > y2 && lastScroll > scroll) {  // 向上
                ref(++start);
                LogUtil.d("向上：y1->" + y1 + "/y2->" + y2 + "/lastScroll->" + lastScroll + "/scroll->" + scroll);
            } else if (lastY < y2 && lastScroll < scroll) { // 向下
                ref(--start);
                LogUtil.d("向下：y1->" + y1 + "/y2->" + y2 + "/lastScroll->" + lastScroll + "/scroll->" + scroll);
            } else {
            }

            lastY = y2;
            lastScroll = scroll;

            scrollTo(0, scroll);

            return true;
        }

        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
            Log.i(getClass().getName(), "onFling-----" + ",(" + e1.getX() + "," + e1.getY() + ") ,(" + e2.getX() + "," + e2.getY() + ")");
            return super.onFling(e1, e2, velocityX, velocityY);
        }
    };

    public void ref(int start) {
        for (int i = 0; i < getChildCount(); i++) {
            View view = getChildAt(i);

            if (view instanceof WheelTextView) {
                ((WheelTextView) view).setText("选项->" + (i + start));
            }
        }
    }

    public class WheelTextView extends AppCompatTextView {
        private int index = -1;

        public WheelTextView(Context context) {
            super(context);
            this.setMaxLines(1);
            this.setGravity(Gravity.CENTER);
            this.setTextSize(wheelTextSize);
            this.setTextColor(wheelTextColor);
            this.setEllipsize(TextUtils.TruncateAt.END);

            this.setText("Sys" + System.currentTimeMillis());
        }

        @Override
        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);
            if (!_isSelected(index)) return;

            Paint paint = new Paint();
            paint.setColor(wheelLineColor);
            paint.setStrokeWidth(wheelLineSize);

            canvas.drawLine(0, 0, getWidth(), 0, paint);
            canvas.drawLine(0, getHeight(), getWidth(), getHeight(), paint);
        }
    }

    public interface WheelDisplay {
        String display();
    }
}
